--[[
      Convert a lua table to a Spring SMD file.
--]]

local function get_indent(amount)
  local spc = ""
  for i=1,amount do
     spc = spc .. "\t"
  end
  return spc
end


local function serialize_smd(smd,file,indent)
  for k,v in pairs(smd) do
    --print(k,v)
    if type(v) == "table" then
      file:write(get_indent(indent) .. string.format("[%s]\n", k))
      file:write(get_indent(indent) .. "{\n")
      indent = indent + 1
      serialize_smd(v,file,indent)
      indent = indent - 1
      file:write(get_indent(indent) .. "}\n")
    elseif type(v) == "string" then
      file:write(get_indent(indent) .. string.format("%s=%s;\n", tostring(k),v))
    elseif type(v) == "number" then
      file:write(get_indent(indent) .. string.format("%s=%d;\n", tostring(k),v))
    end
  end
end

function write_smd(m)
  local out = io.open(m.name .. ".smd", "wb")
  serialize_smd(m.smd,out,0)
  out:close()
end


